package cate.game.world.giftcode;

import cate.common.table.d.GDOperation;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import cp.solution.business.world.giftcode.GiftCodeSrcEntity;
import cp.solution.business.world.giftcode.logic.GiftCodeUseHandler;
import cp.solution.manage.PrimaryClient;
import easy.java.practice.errorcode.EcException;
import easy.java.practice.errorcode.EcResult;

public class MyGiftCodeUseHandler extends GiftCodeUseHandler {

    private final Role role;
    public MyGiftCodeUseHandler(Role role) {
        this.role = role;
    }

    @Override
    protected PrimaryClient getApiClient() {
        return role.getGame().facilities.getApi().pc();
    }

    @Override
    protected boolean hasUsed(String code) {
        return null != role.getWorld().giftCode.getCodeGot().get(code);
    }

    @Override
    protected boolean hasUsedKind(String kind) {
        return null != role.getWorld().giftCode.getKindGot().get(kind);
    }

    @Override
    protected void setCodeUsed(String code) {
        role.getWorld().giftCode.getCodeGot().put(code, now());
    }

    @Override
    protected void setKindUsed(String kind) {
        Integer nKindGot = role.getWorld().giftCode.getKindGot().get(kind);
        nKindGot = nKindGot == null ? 1 : nKindGot+1;
        role.getWorld().giftCode.getKindGot().put(kind, nKindGot);
    }

    @Override
    protected void sendReward(GiftCodeSrcEntity src) throws EcException {
        EcResult rRwd = new MixResAdder().setRes(src.getRewardConf()).setDialogReward(true).setOperation(GDOperation.GIFT_CODE_USE).exe(role);
        if(!rRwd.ok()) {
            throw new EcException(rRwd);
        }
    }

    @Override
    protected String getRoleId() {
        return role.getUid();
    }

    @Override
    protected String getRoleName() {
        return role.getBase().name;
    }

    @Override
    protected Integer getRoleLevel() {
        return role.getBase().level;
    }

    @Override
    protected Integer getRoleVip() {
        return role.getBase().vip;
    }

    @Override
    protected String getGameId() {
        return role.getBase().gameId;
    }

    @Override
    protected String getChannelCode() {
        return role.getBase().channelCode;
    }

    @Override
    protected int getSrvId() {
        return role.getBase().srvId;
    }
}
